package client.ui;

import phasebook.client.msgs.UserProfileInfo;
import client.Controller;
import client.ui.ImagePicker.PickListener;

import com.smartgwt.client.types.ImageStyle;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.RichTextEditor;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStackSection;

public class EditorSection extends SectionStackSection implements PickListener{
	private RichTextEditor richTextEditor;
	private Long imageId = null;
	private HLayout hLayout;

	public EditorSection(final Controller controller, final UserProfileInfo user,final ImagePicker imagePicker) {
		super("Message Editor");
		super.setExpanded(true);
		super.setResizeable(true);

		richTextEditor = new RichTextEditor();
		richTextEditor.setHeight100();
		richTextEditor.setWidth100();
		richTextEditor.setOverflow(Overflow.AUTO);
		richTextEditor.setCanDragResize(true);
		richTextEditor.setValue("write your message here");
		
		ImgButton publishPublic = new ImgButton();
		publishPublic.setSrc("[SKIN]actions/add.png");
		publishPublic.setTitle("Publish Public");
		publishPublic.setTooltip("Publish Public");
		publishPublic.setSize(16);
		publishPublic.setShowFocused(false);
		publishPublic.setShowRollOver(false);
		publishPublic.setShowDown(false);
		publishPublic.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				controller.publishMessage(user.getUserId(), richTextEditor.getValue(), false, imageId);
			}
		});
		
		ImgButton publishPrivate = new ImgButton();
		publishPrivate.setSrc("[SKIN]actions/remove.png");
		publishPrivate.setTitle("Publish Private");
		publishPrivate.setTooltip("Publish Private");
		publishPrivate.setSize(16);
		publishPrivate.setShowFocused(false);
		publishPrivate.setShowRollOver(false);
		publishPrivate.setShowDown(false);
		publishPrivate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				controller.publishMessage(user.getUserId(), richTextEditor.getValue(), true, imageId);
			}
		});
		
		hLayout = new HLayout();
		
		ImgButton attachImage = new ImgButton();
		attachImage.setSrc("[SKIN]actions/color_swatch.png");
		attachImage.setTitle("Attach Picture");
		attachImage.setTooltip("Attach Picture");
		attachImage.setSize(16);
		attachImage.setShowFocused(false);
		attachImage.setShowRollOver(false);
		attachImage.setShowDown(false);
		attachImage.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				imagePicker.show();
			}
		});
		
		onImagePicked(null);

		super.setControls(publishPublic,publishPrivate,attachImage);
		super.addItem(hLayout);
		
		imagePicker.addPickListener(this);
	}

	@Override
	public void onImagePicked(Long imgId) {
		String currentText = richTextEditor.getValue();
		hLayout.setMembers(new Canvas[0]);
		hLayout.addMember(richTextEditor);
		this.imageId = imgId;
		
		if (imgId!=null){
			Img img = new Img("image?id="+imgId,320,180);
			img.setImageType(ImageStyle.STRETCH);
			hLayout.addMember(img);
		}
		richTextEditor.setValue(currentText);
	}
}
